package com.apache.nio.channel;

import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 从本地文件读取信息
 */
public class NIOFileChannel02 {
  public static void main(String[] args) throws Exception {

    // 创建文件的输入流
    File file = new File("d:\\file01.txt");
    FileInputStream fileInputStream = new FileInputStream(file);

    // 通过fileInputStream 获取对应的FileChannel -> 实际类型  FileChannelImpl
    FileChannel fileChannel = fileInputStream.getChannel();

    // 创建缓冲区
    ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());

    // 将 通道的数据读入到Buffer
    fileChannel.read(byteBuffer);

    // 将byteBuffer 的 字节数据 转成String
    System.out.println(new String(byteBuffer.array()));
    fileInputStream.close();
  }
}
